#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
int main() {
	int n;
	scanf("%d", &n);
	int ret = (3 * n);
	int a_i[ret];
	int i = 0;
	while (ret--) {
		scanf("%d", &a_i[i++]);
	}
	int b = 0;
	int sum = 0;
	while (b < (3 * n)) {
		int poi;
		for (i = b; i < b + 3; i++) {
			if ((i == (b + 0)) && (a_i[i] >= a_i[i + 1] && a_i[i] <= a_i[i + 2]) || (a_i[i] <= a_i[i + 1] && a_i[i] >= a_i[i + 2]))
			{
				poi = a_i[i];
				break;
			}
			if ((i == (b + 1)) && (a_i[i] >= a_i[i - 1] && a_i[i] <= a_i[i + 1]) || (a_i[i] <= a_i[i - 1] && a_i[i] >= a_i[i + 1]))
			{
				poi = a_i[i];
				break;
			}
			if ((i == (b + 2)) && (a_i[i] >= a_i[i - 2] && a_i[i] <= a_i[i - 1]) || (a_i[i] <= a_i[i - 2] && a_i[i] >= a_i[i - 1]))
			{
				poi = a_i[i];
				break;
			}
		}
		sum += poi;
		b += 3;
	}
	printf("%d", sum);
	return 0;
}
